home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Processes / ProcDoggie2.1b2 / ProcDoggie.p < prev    next >
Encoding:
Text File  |  1997-11-03  |  10.1 KB  |  327 lines  |  [TEXT/CWIE]

  1. PROGRAM ProcDoggie;
  2.  
  3. {-------------------------------------------------------------------------------
  4.     File:        ProcDoggie.p
  5.  
  6.     Contains:    Main program file for the ProcDoggie application.
  7.  
  8.     Written by:    Forrest Tanaka
  9.  
  10.     Copyright:    © 1988-1997 by Apple Computer, Inc., all rights reserved.
  11.  
  12.     Change History (most recent first):
  13.  
  14.     You may incorporate this sample code into your applications without
  15.     restriction, though the sample code has been provided "AS IS" and the
  16.     responsibility for its operation is 100% yours.  However, what you are
  17.     not permitted to do is to redistribute the source as "DSC Sample Code"
  18.     after having made changes. If you're going to re-distribute the source,
  19.     we require that you make it clear in the source that the code was
  20.     descended from Apple Sample Code, but that you've made changes.
  21. --------------------------------------------------------------------------------
  22. #
  23. #    ProcDoggie.p is the root file for ProcDoggie.  It contains the main entry
  24. #    point and the PROGRAM statement, but relies on the other source files
  25. #    included with this application to actually implement the functionality.
  26. #
  27. #    This file itself contains the following functionality:
  28. #    1. startup code
  29. #    2. AppleEvent handlers
  30. #    3. main event loop
  31. #
  32. #    UEvent.p is the unit responsible for actually fielding events.
  33. #
  34. -------------------------------------------------------------------------------}
  35. {[j=20/57/1$] Pasmat Options}
  36.     
  37.  
  38. (*******************************************************************************
  39. * Used Units
  40. *******************************************************************************)
  41.  
  42.     USES
  43.         AppleEvents
  44.         ,Fonts
  45.         ,ToolUtils
  46.         ,DiskInit
  47.         ,SegLoad
  48.         ,GestaltEqu
  49.         
  50.         (* Application *)
  51.         ,UGlobals
  52.         ,UEmergMem
  53.         ,UProcessUtils
  54.         ,UMenuHandler
  55.         ,UProcessGuts
  56.         ,UEvents
  57.         ;
  58.  
  59.  
  60. (*******************************************************************************
  61. * Constants
  62. *******************************************************************************)
  63.  
  64.     CONST
  65.         kBecomingActive = TRUE; {Pass to DoActivateEvt; indicates becoming active}
  66.  
  67.  
  68. (*******************************************************************************
  69. * Global Variables
  70. *******************************************************************************)
  71.  
  72.     VAR
  73.         gProcessListWind: WindowPtr; {Pointer to the process list window}
  74.  
  75.  
  76. {$S Main}
  77. (*******************************************************************************
  78. * DoneRequiredParams - Done processing required params; OK?
  79. *
  80. * DoneRequiredParams checks to see if the AppleEvent specified by the
  81. * anAppleEvent parameter has any required parameters that we haven’t yet
  82. * processed.  If there aren’t any left, then noErr is returned.  If there are
  83. * required parameters that haven’t been processed yet, then errAEEventNotHandled
  84. * is returned.  If any other errors occur, then that error code is returned.
  85. *******************************************************************************)
  86.  
  87.     FUNCTION DoneRequiredParams (anAppleEvent: AppleEvent): OSErr;
  88.  
  89.         VAR
  90.             typeCode:   DescType; {Type of AppleEvent attribute found; ignored}
  91.             actualSize: Size;     {Actual size of parameters; ignored}
  92.             error:      OSErr;
  93.  
  94.     BEGIN
  95.         (* Are there any required parameters in AppleEvent we didn’t process? *)
  96.            error := AEGetAttributePtr (anAppleEvent, keyMissedKeywordAttr,
  97.                     typeWildCard, (*<*)typeCode, NIL, 0, (*<*)actualSize);
  98.             IF error = errAEDescNotFound THEN
  99.                 (* No required parameters left, so no error *)
  100.                 DoneRequiredParams := noErr
  101.             ELSE IF error = noErr THEN
  102.                 (* There was at least one required parameter we didn’t process *)
  103.                 DoneRequiredParams := errAEEventNotHandled
  104.             ELSE
  105.                 (* Some other error happened *)
  106.                 DoneRequiredParams := error
  107.     END;
  108.  
  109.  
  110. {$S Main}
  111. (*******************************************************************************
  112. * HandleAEquit - Handler for 'quit' AppleEvent
  113. *
  114. * This is the AppleEvent handler for the 'quit' AppleEvent as passed in the
  115. * quitAppleEvent parameter by the AppleEvent Manager.  The DoQuit routine is
  116. * called which causes this application to quit at the start of the next
  117. * iteration of the main event loop.
  118. *
  119. * Though the quit AppleEvent doesn’t contain any parameters, the standard thing
  120. * to do in reaction to any AppleEvent is to check to see if there are any
  121. * required parameters in the AppleEvent that this routine doesn’t recognise.
  122. * DoneRequiredParms checks for this condition and returns an error if there are
  123. * in fact required parameters in the AppleEvent or if some other error occurs
  124. * during the check.
  125. *******************************************************************************)
  126.  
  127.     FUNCTION HandleAEquit (VAR quitAppleEvent: AppleEvent;
  128.                            VAR reply:          AppleEvent;
  129.                            handlerRefCon:  LongInt): OSErr;
  130.  
  131.         VAR
  132.             error: OSErr;
  133.  
  134.         PROCEDURE RecoverError (errorCode: OSErr);
  135.  
  136.         BEGIN
  137.             HandleAEquit := errorCode;
  138.             EXIT (HandleAEquit)
  139.         END;
  140.  
  141.     BEGIN
  142.         {$unused reply}
  143.         {$unused handlerRefCon}
  144.         (* quit AE has no parms, but check in case the client requires any *)
  145.         error := DoneRequiredParams (quitAppleEvent);
  146.         IF error <> noErr THEN
  147.             RecoverError (error);
  148.  
  149.         (* Handle the Quit command *)
  150.         error := DoQuit;
  151.         IF error <> noErr THEN
  152.             RecoverError(error);
  153.  
  154.         HandleAEquit := noErr
  155.     END;
  156.  
  157.  
  158. {$S Startup}
  159. (*******************************************************************************
  160. * StartUp - Do whatever has to be done to initialize the application
  161. *
  162. * This routine is called after the heap is initialized to initialize the
  163. * application.  This involves initializing the toolbox, emergency memory, and
  164. * loading up the menus.  If any errors occur while doing this, StartUp displays
  165. * an alert telling the user what the error was and then ExitToShell is called.
  166. * This is an unusual way to react to errors, and I only do it here because it’s
  167. * so early in execution that there really isn’t much else that can be done.
  168. *
  169. * See this UEmergMem unit in this application for details about emergency
  170. * memory.
  171. *******************************************************************************)
  172.  
  173.     PROCEDURE StartUp;
  174.  
  175.         CONST
  176.             kSysHandler = TRUE; {Specifies that AE handler is in system heap}
  177.  
  178.         VAR
  179.             error: OSErr;
  180.  
  181.         PROCEDURE HandleError (messageClass: Integer;
  182.                                messageIndex: Integer);
  183.  
  184.             VAR
  185.                 junkError : OSErr;
  186.                 junkItemHit: Integer; {Result of alert; ignored}
  187.  
  188.         BEGIN
  189.             junkError := ShowStopAlert (messageClass, messageIndex, junkItemHit);
  190.             ExitToShell
  191.         END;
  192.         
  193.         VAR
  194.             gestaltResponse: LongInt;
  195.  
  196.     BEGIN
  197.         (* Initialize the toolbox *)
  198.         InitGraf (@qd.thePort);
  199.         InitFonts;
  200.         InitWindows;
  201.         InitMenus;
  202.         TEInit;
  203.         InitDialogs (NIL);
  204.         
  205.         (* Check environment. *)
  206.         IF (Gestalt(gestaltSystemVersion, gestaltResponse) <> noErr) | ( gestaltResponse < $0700) THEN
  207.             HandleError(rMiscErrMessages, kMiscSystemTooSmall);
  208.  
  209.         (* We make the assumption that if we have System 7 then we have a bunch of other
  210.             System 7 features that we rely on, namely:
  211.             
  212.             o new Standard File calls
  213.             o AppleEvents
  214.             o Process Manager
  215.         
  216.           While this is counter to the philosophy of Gestalt, it sure makes the code smaller.
  217.             It's also a sensible approach for "big bang" system releases like System 7.
  218.             Besides, ProcDoggie can do nothing useful without the Process Manager (which
  219.             was introduced with System 7), so there's no point in us 'limping along',
  220.             using old style Standard File calls and so on, when we can't display a
  221.             process list.
  222.         *)
  223.         
  224.         (* Initialise some boring parts of our application.  Basically this involves
  225.             making some UPPs.
  226.         *)
  227.         InitGlobals;
  228.         InitProcessGuts;
  229.  
  230.         (* Initialize emergency memory *)
  231.         InitEmergMem;
  232.         IF FailLowMemory (0) THEN
  233.             HandleError (rMemErrMessages, kMemErrAppOpenMsg);
  234.  
  235.         (* Load the menus and draw the menu bar *)
  236.         StartMenus;
  237.         IF FailLowMemory (0) THEN
  238.             HandleError (rMemErrMessages, kMemErrAppOpenMsg)
  239.         ELSE IF gError <> noErr THEN
  240.             IF gError = memFullErr THEN
  241.                 HandleError (rMemErrMessages, kMemErrAppOpenMsg)
  242.             ELSE IF gError = resNotFound THEN
  243.                 HandleError (rResErrMessages, kResErrAppDamageMsg)
  244.             ELSE
  245.                 HandleError (rMiscErrMessages, kMiscErrUnknownMsg);
  246.  
  247.         (* Install the AppleEvent handler *)
  248.         error := AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
  249.                 NewAEEventHandlerProc(@HandleAEquit), 0, NOT kSysHandler);
  250.         IF (error = memFullErr) | FailLowMemory (0) THEN
  251.             HandleError (rMemErrMessages, kMemErrAppOpenMsg)
  252.         ELSE IF error <> noErr THEN
  253.             HandleError (rMiscErrMessages, kMiscErrUnknownMsg)
  254.     END;
  255.  
  256.  
  257. {$S Main}
  258. (*******************************************************************************
  259. * EventLoop - Main event loop for this application
  260. *
  261. * This is the main event loop of this application.  During every iteration of
  262. * the event loop, the menus are kept up-to-date, and the Process List window and
  263. * all of the open Process Information windows are given time to update
  264. * themselves to current conditions.  Also, NoEmergMem is called to detect
  265. * whether the emergency memory was used.  If it was, then RecoverEmergMem is
  266. * called in an attept to get it back.  If it can’t, then some commands could be
  267. * disabled until the memory can be recovered.
  268. *******************************************************************************)
  269.  
  270.     PROCEDURE EventLoop;
  271.  
  272.         VAR
  273.             anEvent: EventRecord; {An incoming event}
  274.  
  275.     BEGIN
  276.         FixMenus;
  277.         InitCursor;
  278.         gQuitting := FALSE;
  279.  
  280.         (* We loop “forever,” or until the Quit handler calls ExitToShell *)
  281.         WHILE NOT gQuitting DO
  282.             BEGIN
  283.                 (* Give all open windows some time *)
  284.                 IdleAllProcessWindows;
  285.  
  286.                 (* Try to reallocate emergency memory if it’s been used *)
  287.                 IF NoEmergMem THEN
  288.                     RecoverEmergMem;
  289.  
  290.                 (* Fix the menus to reflect current conditions *)
  291.                 FixMenus;
  292.  
  293.                 (* It’s time to get and examine an event *)
  294.                 IF WaitNextEvent (everyEvent, (*<*)anEvent, kMaxSleepTime, NIL) THEN
  295.                     BEGIN
  296.                         DoEvent(anEvent);
  297.                     END
  298.             END
  299.     END;
  300.  
  301.  
  302. BEGIN
  303.     (* Set up the heap *)
  304.     MaxApplZone;
  305.     MoreMasters;
  306.     MoreMasters;
  307.     MoreMasters;
  308.     MoreMasters;
  309.     MoreMasters;
  310.     MoreMasters;
  311.  
  312.     (* Do anything that must be done at program start-up *)
  313.     StartUp;
  314.     {$ifc not GENERATINGCFM}
  315.         UnloadSeg (@StartUp);
  316.     {$endc}
  317.  
  318.     (* Set the default launch mode *)
  319.     SetLaunchMode (kJustLaunch);
  320.  
  321.     (* Open the process list window *)
  322.     gProcessListWind := CreateProcessListWindow;
  323.  
  324.     (* Enter the main event loop *)
  325.     EventLoop
  326. END.
  327.